package cn.zwxky.android.staff.Jurisdiction.Operate;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.Event.LsfbEvent;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInjectUtils;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.SN;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.zwxky.android.staff.Jurisdiction.Marketplace.TaskApproveClickEvent;
import cn.zwxky.android.staff.Jurisdiction.Marketplace.TaskApproveClickEvent2;
import cn.zwxky.android.staff.Jurisdiction.Marketplace.TaskApproveListAdapter;
import cn.zwxky.android.staff.Jurisdiction.Marketplace.TaskApproveListBean;
import cn.zwxky.android.staff.R;
import cn.zwxky.android.staff.Utils.LittleUtils;
import cn.zwxky.android.staff.Utils.URLString;

import static cn.zwxky.android.staff.Utils.LittleUtils.select;


/**
 * Created by Administrator on 2017/3/31 0031.
 */

public class OperateTaskApprove extends Fragment {
    /**
     * view对象
     */
    private View view;

    /**
     * activity对象
     */
    private Activity mactivity;

    /**
     * 点击刷新布局控件
     */
    @ViewInject(R.id.swiplayout)
    private SwipeRefreshLayout swipeRefreshLayout;

    /**
     * list列表
     */
    @ViewInject(R.id.dish_list)
    private RecyclerView dish_list;

    /**
     * RecyclerView的布局管理器
     */
    private RecyclerView.LayoutManager mlayoutManager;

    private List<TaskApproveListBean.ListBean> mList;

    private TaskApproveListAdapter adapter;


    @ViewInject(R.id.aty_task_approve_unfinish)
    private TextView unfinish;

    @ViewInject(R.id.aty_task_approve_finish)
    private TextView finish;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_task_approve, null);
        ViewInjectUtils.inject(this, view);

        mList = new ArrayList<>();
        adapter = new TaskApproveListAdapter(mactivity, mList, R.layout.item_task_approve);
        mlayoutManager = new LinearLayoutManager(mactivity);//布局模式为listview方式
        dish_list.setLayoutManager(mlayoutManager);
        dish_list.setAdapter(adapter);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(select);
            }
        });
        LittleUtils.select = 0;
        getData(0);

        return view;
    }

    /**
     * 用于存储activity对象
     *
     * @param activity
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mactivity = activity;
    }

    @OnClick({R.id.aty_task_approve_unfinish, R.id.aty_task_approve_finish})
    public void onClick(View view) {
        switch (view.getId()) {
            //待讨论
            case R.id.aty_task_approve_unfinish:
                LittleUtils.select = 0;
                unfinish.setTextColor(getResources().getColor(R.color.white_color));
                unfinish.setBackgroundResource(R.drawable.aty_task_approve_left_bg);
                finish.setTextColor(getResources().getColor(R.color.theme_color));
                finish.setBackgroundResource(R.drawable.aty_task_approve_right_bai);
                getData(0);
                break;
            //已发布
            case R.id.aty_task_approve_finish:
                LittleUtils.select = 1;
                unfinish.setTextColor(getResources().getColor(R.color.theme_color));
                unfinish.setBackgroundResource(R.drawable.aty_task_approve_left_bai);
                finish.setTextColor(getResources().getColor(R.color.white_color));
                finish.setBackgroundResource(R.drawable.aty_task_approve_right_bg);
                getData(1);
                break;
        }
    }

    /**
     * 审批列表
     */
    public void getData(int type) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        hashMap.put("type", String.valueOf(type));//(仅限经营部部有效) 不传或传0为讨论，1为已发布
        new BaseInternet().getData_Get(URLString.MANAGE_MYEAALIST, hashMap, TaskApproveListBean.class, new TaskApproveListEvent2<TaskApproveListBean>(), true, true);
    }

    @EventAnnotation
    public void TaskApproveListEvent(TaskApproveListEvent2<TaskApproveListBean> event) {
        swipeRefreshLayout.setRefreshing(false);
        switch (event.getCode()) {
            case 200:
                mList.clear();
                mList.addAll(event.getData().getList());
                adapter.notifyDataSetChanged();
                break;
            default:
                mList.clear();
                adapter.notifyDataSetChanged();
                SN.SnackShow(dish_list, event.getMsg());
                break;
        }
    }

    /**
     * 跳转审批详情
     *
     * @param event
     */
    @EventAnnotation
    public void TaskApproveClickEvent(TaskApproveClickEvent event) {
        //经营部
      startActivity(new Intent(mactivity,OperateTaskApproveDetailsActivity.class).putExtra("id", event.getData()).putExtra("type",LittleUtils.select));
    }
    /**
     * 跳转审批详情
     *
     * @param event
     */
    @EventAnnotation
    public void TaskApproveClickEvent2(TaskApproveClickEvent2 event) {
        //经营部
      startActivity(new Intent(mactivity,OperateTaskApproveDetailsActivity.class).putExtra("id", event.getData()).putExtra("type",0));
    }



    @Override
    public void onResume() {
        super.onResume();
        getData(LittleUtils.select);
        LsfbEvent.getInstantiation().register2(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        LsfbEvent.getInstantiation().unregister2(this);
    }



}
